package com.fullstack.server.endpoints

import cats.effect.Sync
import com.fullstack.common.domain.error.ErrorInfo
import com.fullstack.common.domain.user.{UpdateUserInfo, UserId, UserInfo}
import com.fullstack.server.service.UserService
import sttp.tapir.server.{PartialServerEndpoint, ServerEndpoint}
import sttp.tapir.*
import sttp.tapir.generic.auto.*
import sttp.tapir.json.circe.*
import io.circe.generic.auto.*



class UserEndpoint [F[_]: Sync](userService: UserService[F]) extends BaseEndpoint[F]:

  // 查看自己用户信息
  private val userInfoEndpoint: PublicEndpoint[UserId, ErrorInfo, UserInfo, Any] =
    baseEndpoint.post
      .in("user" / "getUserInfo")
      .in(jsonBody[UserId])
      .out(jsonBody[UserInfo])
  val userInfoServerEndpoint: ServerEndpoint[Any, F] = userInfoEndpoint.serverLogic(userId => userService.getUserInfo(userId.id))


  // 新增用户
  private val addUserEndpoint:  PublicEndpoint[UserInfo, ErrorInfo, UserInfo, Any] =
    baseEndpoint.post
      .in("user" / "addUser")
      .in(jsonBody[UserInfo])
      .out(jsonBody[UserInfo])
  val addUserServerEndpoint: ServerEndpoint[Any, F] = addUserEndpoint.serverLogic(userInfo => userService.addUser(userInfo))


  // 更改用户信息
  private val updateUserInfoEndpoint: PublicEndpoint[UserInfo, ErrorInfo, UserInfo, Any] =
    baseEndpoint.post
      .in("user" / "updateUserInfo")
      .in(jsonBody[UserInfo])
      .out(jsonBody[UserInfo])
  val updateUserInfoServerEndpoint: ServerEndpoint[Any, F] = updateUserInfoEndpoint.serverLogic(updateUserInfo => userService.updateUserInfo(updateUserInfo))


  
  val all = List(
    userInfoServerEndpoint,
    updateUserInfoServerEndpoint,
  )

end UserEndpoint


object UserEndpoint:

end UserEndpoint

